package parser;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PopulateTableParser {
	
	public static final int NUMBER_PLAYERS_PER_ROUND = 352;
	
	public static final int NAME = 1; //1;
	public static final int TEAM = 12; //2;
	public static final int SCORE = 11;
	
	private Connection connection;
	private Map<String, String> teams;
	private List<Player> players;
	private int preSeasonRound = -1;
	
	public void init() {
		teams = new HashMap<String, String>();
		populateTeams(teams);
		populatePlayers();
		establishConnection();
	}
	
	public static void main(String[] args) {
		PopulateTableParser playerScoreParser = new PopulateTableParser();

		playerScoreParser.run();
	}

	public void run() {
		establishConnection();
		
		BufferedReader bufRead = null;
		try {
	        FileReader input = new FileReader("c:/playerscore.csv");
			bufRead = new BufferedReader(input);
			String line = "";
			int count = 0;
			Statement stmt = connection.createStatement();
			
			while ((line = bufRead.readLine()) != null && count < 5) {
				String[] components = line.split(",");
				
				//stmt.executeUpdate("INSERT INTO PlayerForRound VALUES (" + components[0].trim() + "," + (components[1]==null?"null":components[1]) + "," + (components[2].equals("")?"null":components[2]) + "," + components[3].trim() + ", '" + (components.length==5?components[4]:"") + "')");
				stmt.executeUpdate("INSERT INTO PlayerScore VALUES (" + components[0].trim() + "," + components[1] + "," + components[2] + "," + components[3] + ")");
				
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			try {
				bufRead.close();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private static void populateTeams(Map<String, String> teams) {
		teams.put("West Coast", "WCE");
		teams.put("Richmond", "RICHMOND");
		teams.put("Geelong", "GEELONG");
		teams.put("Melbourne", "MELBOURNE");
		teams.put("Port Adelaide", "PORT_ADELAIDE");
		teams.put("St Kilda", "ST_KILDA");
		teams.put("Adelaide", "ADELAIDE");
		teams.put("North Melbourne", "NORTH_MELBOURNE");
		teams.put("Fremantle", "FREMANTLE");
		teams.put("Hawthorn", "HAWTHORN");
		teams.put("Brisbane", "BRISBANE");
		teams.put("Western Bulldogs", "WESTERN_BULLDOGS");
		teams.put("Collingwood", "COLLINGWOOD");
		teams.put("Essendon", "ESSENDON");
		teams.put("Sydney", "SYDNEY");
		teams.put("Carlton", "CARLTON");
	}
	
	private void establishConnection() {
		try {
			Class.forName( "com.mysql.jdbc.Driver" );
			connection = DriverManager.getConnection(
				     "jdbc:mysql://localhost/sffdraftgame",
				     "root",
				     "password" );

		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void SetPreSeasonRound(int preSeasonRound) {
		this.preSeasonRound = preSeasonRound;
	}
	
	public void populatePlayers() {
		players = new ArrayList<Player>();
		players.add(new Player("Lachie", "Henderson", "Carlton", "Lachlan", "Henderson"));
		players.add(new Player("Chris", "Yarran", "Carlton", "Christopher", "Yarran"));
		players.add(new Player("Scott", "D. Thompson", "North Melbourne", "Scott", "Thompson"));
		players.add(new Player("Sam", "Wright", "North Melbourne", "Samuel", "Wright"));
		players.add(new Player("Josh", "P. Kennedy", "Sydney", "Josh", "Kennedy"));
		players.add(new Player("L.", "Roberts-Thomson", "Sydney", "Lewis", "Roberts-Thomson"));
		players.add(new Player("Matt", "Maguire", "Brisbane", "Matthew", "Maguire"));
		players.add(new Player("S.", "Prestigiacomo", "Collingwood", "Simon", "Prestigiacomo"));
		players.add(new Player("Ben", "Stratton", "Hawthorn", "Benjamin", "Stratton"));
		players.add(new Player("Jefferey", "Garlett", "Carlton", "Jeff", "Garlett"));
		players.add(new Player("Lachie", "Hansen", "North Melbourne", "Lachlan", "Hansen"));
		players.add(new Player("Scott D.", "Thompson", "North Melbourne", "Scott", "Thompson"));
		players.add(new Player("Josh P.", "Kennedy", "Sydney", "Josh", "Kennedy"));
		players.add(new Player("Josh J.", "Kennedy", "West Coast", "Joshua", "Kennedy"));
		players.add(new Player("Nick", "Naitanui", "West Coast", "Nicholas", "Naitanui"));
		players.add(new Player("Matt", "Priddis", "West Coast", "Matthew", "Priddis"));
		players.add(new Player("Matt", "Rosa", "West Coast", "Matthew", "Rosa"));
		players.add(new Player("Matt", "Spangher", "West Coast", "Matthew", "Spangher"));
		players.add(new Player("Ricky", "Dyson", "Essendon", "Rick", "Dyson"));
		players.add(new Player("Matt", "White", "Richmond", "Matthew", "White"));
		players.add(new Player("Mitch", "Duncan", "Geelong", "Mitchell", "Duncan"));
		players.add(new Player("Robbie", "Gray", "Port Adelaide", "Robert", "Gray"));
		players.add(new Player("Chris", "Mayne", "Fremantle", "Christopher", "Mayne"));
		players.add(new Player("Mitch", "Clark", "Brisbane", "Mitchell", "Clark"));
		players.add(new Player("Nathan J.", "Brown", "Collingwood", "Nathan", "Brown"));
		
		 
		
	}
	
	class Player {
		private String firstName;
		private String surname;
		private String team;
		private String requiredFirstName;
		private String requiredSurname;
		
		public Player(String firstName, String surname, String team, String requiredFirstName, String requiredSurname) {
			super();
			this.firstName = firstName;
			this.surname = surname;
			this.team = team;
			this.requiredFirstName = requiredFirstName;
			this.requiredSurname = requiredSurname;
		}

		public String getFirstName() {
			return firstName;
		}
		
		public void setFirstName(String firstName) {
			this.firstName = firstName;
		}
		
		public String getSurname() {
			return surname;
		}
		
		public void setSurname(String surname) {
			this.surname = surname;
		}
		
		public String getTeam() {
			return team;
		}
		
		public void setTeam(String team) {
			this.team = team;
		}

		public String getRequiredFirstName() {
			return requiredFirstName;
		}

		public void setRequiredFirstName(String requiredFirstName) {
			this.requiredFirstName = requiredFirstName;
		}

		public String getRequiredSurname() {
			return requiredSurname;
		}

		public void setRequiredSurname(String requiredSurname) {
			this.requiredSurname = requiredSurname;
		}
		
	}
}
